<?php
$url		= $_SERVER['PATH_INFO'];

$max_width	= isset($_GET["w"])?$_GET["w"]:368;
$max_height	= isset($_GET["h"])?$_GET["h"]:2000;


//Source Address
$source_photo = "files/".$url;

// Check
if (!$url) { exit("no url"); }
if (!file_exists($source_photo)) { exit("no file"); }



$getImg = getImageSize($source_photo);
$img_width	= $getImg[0];
$img_height	= $getImg[1];
$img_type	= $getImg[2];

// type 1~3만 이미지파일.
if($img_type < 4 && $img_type > 0)header("Content-Type: image/jpeg");
else exit();

$newimg = imagecreatetruecolor($img_width, $img_height);
if($img_type==1) $intoimg = imagecreatefromgif($source_photo);
else if($img_type==2) $intoimg = imagecreatefromjpeg($source_photo);
else if($img_type==3) $intoimg = imagecreatefrompng($source_photo);

imagecopymerge($newimg, $intoimg, 0, 0, 0, 0, $img_width, $img_height, 100);
imagedestroy($intoimg);

if($img_width > $max_width || $img_height > $max_height){
	$nimg_width = round($img_width * $max_height / $img_height);
	$nimg_height = $max_height;
	if ($nimg_width > $max_width) {
		$nimg_width = $max_width;
		$nimg_height = round($img_height * $max_width / $img_width);
	}
}
else{
	$nimg_width = $img_width;
	$nimg_height = $img_height;
}

$nimg = imagecreatetruecolor($nimg_width, $nimg_height); 
imagecopyresampled($nimg, $newimg, 0, 0, 0, 0, $nimg_width, $nimg_height, $img_width, $img_height);
imagedestroy($newimg);


imagejpeg($nimg);
imagedestroy($nimg);
?>